<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Proxy 与 Reflect</title>
</head>
<body>
  <script>
    // 目标数据对象
    const user = {
      name: 'tom',
      age: 12
    }

    // 创建一个代理对象
    const userProxy = new Proxy(user, {
      // 监视读取属性
      get (target, property) {
        console.log('拦截到读取属性', property)
        return Reflect.get(target, property)
      },
      // 监视设置属性或添加属性
      set (target, property, value) {
        console.log('拦截到设置属性或添加属性')
        return Reflect.set(target, property, value) 
      },

      // 监视删除属性
      deleteProperty (target, property) {
        console.log('拦截到删除属性', property)
        return Reflect.deleteProperty(target, property)
      }
    })

    // 测试
    
    // 读取属性
    // console.log(userProxy.name)

    // 设置新的属性值
    // userProxy.name = 'abcd'

    // 添加新属性
    // userProxy.sex = '男'

    // 删除属性
    delete userProxy.sex
    
    console.log(user)



  </script>
</body>
</html>